package com.lhd.web;

import com.lhd.po.Tag;
import com.lhd.po.Type;
import com.lhd.service.TagService;
import com.lhd.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@RestController
public class AllRestController {

    @Autowired
    TypeService typeService;

    @Autowired
    TagService tagService;

    @GetMapping("/getType")
    public List<Type> getType(){
        return typeService.listType();
    }

    @GetMapping("/admin/type/{id}/delete")
    public Long deleteType(@PathVariable Long id) {
        return typeService.deleteType(id);
    }

    @PostMapping("/admin/type/post")
    public Integer postType(@RequestBody Type type){
        return type.getId() < 0 ? typeService.saveType(type) : typeService.updateType(type.getId(),type);
    }

    @GetMapping("/getTag")
    public List<Tag> getTag(){
        return tagService.listTag();
    }

    @GetMapping("/admin/tag/{id}/delete")
    public Long deleteTag(@PathVariable Long id) {
        return tagService.deleteTag(id);
    }

    @PostMapping("/admin/tag/post")
    public Integer postTag(@RequestBody Tag tag){
        return tag.getId() < 0 ? tagService.saveTag(tag) : tagService.updateTag(tag.getId(),tag);
    }

}
